package com.ophiux.cs.application.token.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ophiux.cs.application.core.Result;
import com.ophiux.cs.application.core.ResultEnum;
import com.ophiux.cs.application.core.Throws.ParamException;
import com.ophiux.cs.application.token.dto.verUserTokenRequest;
import com.ophiux.cs.application.token.dto.verUserTokenResponse;
import com.ophiux.cs.application.token.service.IVerTokenService;
import com.ophiux.cs.domain.user.model.entity.UserLoginRecore;
import com.ophiux.cs.domain.user.repositories.IUserRecoreRepository;

import lombok.extern.slf4j.Slf4j;
@Service
@Slf4j
public class VerTokenService  implements IVerTokenService{
	@Autowired
	IUserRecoreRepository iUserRecoreRepository;//用户登录记录仓储
	
	@Override
	public Result<verUserTokenResponse> verUserToken(verUserTokenRequest dto) {
		Result<verUserTokenResponse> result = new Result<verUserTokenResponse>();
		verUserTokenResponse res = new verUserTokenResponse();
		try {
			try {
				//验证参数是否允许为空
				dto.verParam();
			} catch (ParamException e) {
				return Result.fail(ResultEnum.requestException.getCode(), e.getMessage()); 
			}
			
			/**
			 * 根据用户唯一标识判断 是否存在token信息
			 */
			String token = dto.getTOKEN();
			
			UserLoginRecore userRecore = iUserRecoreRepository.getUserRecoreByToken(token);
			
			if(userRecore != null) {//存在当前token有效登录记录
				res.setResult(true);
			}else {//无当前用户的有效登录token
				res.setResult(false);
//				return Result.fail(ResultEnum.TokenFail.getCode(), ResultEnum.TokenFail.getMsg());
			}
			result = Result.success(res);
		} catch (Exception e) {
			log.info("addUserInfo接口异常：",e);
			e.printStackTrace();
			return Result.fail(ResultEnum.requestException.getCode(), ResultEnum.requestException.getMsg());
		}
		
		
		return result;
	}

}
